package com.syn.synold4s.control;


import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.inputmethod.EditorInfo;

import androidx.appcompat.widget.AppCompatEditText;
import androidx.core.content.ContextCompat;

import com.syn.synold4s.R;

/**
 * 自定义的EditText右边带图片，可以设置点击事件
 */
public class ClearEditText extends AppCompatEditText {

    private Drawable mDeleteDrawable;
    private OnAfterTextChangedListener mTextChangedListener;
    private OnActionSearchListener mActionSearchListener;

    /**
     * 内容改变监听
     */
    public void setOnAfterTextChangedListener(OnAfterTextChangedListener listener) {
        this.mTextChangedListener = listener;
    }

    /**
     * 键盘搜索键监听
     */
    public void setActionSearchListener(OnActionSearchListener listener) {
        this.mActionSearchListener = listener;
    }

    public ClearEditText(Context context) {
        super(context);
        init(context);
    }

    public ClearEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public ClearEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        mDeleteDrawable = getCompoundDrawables()[2];
        if (mDeleteDrawable == null) {
            mDeleteDrawable = ContextCompat.getDrawable(context, R.drawable.ic_delete);
        }
        if (mDeleteDrawable != null) {
            //设置mDeleteDrawable占的面积
            mDeleteDrawable.setBounds(0, 0, mDeleteDrawable.getIntrinsicWidth(),
                    mDeleteDrawable.getIntrinsicHeight());
        }
        // 设置输入框里面内容发生改变的监听
        addTextChangedListener(watcher);
        setOnEditorActionListener((textView, actionId, keyEvent) -> {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                if (mActionSearchListener != null) {
                    mActionSearchListener.actionSearch(getText() == null ? "" : getText().toString());
                    return true;
                }
            }
            return false;
        });
    }

    private final TextWatcher watcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            setClearIconVisible(s.length() > 0);
        }

        @Override
        public void afterTextChanged(Editable s) {
            if (mTextChangedListener != null) {
                mTextChangedListener.afterTextChanged(s.toString());
            }
        }
    };

    private void setClearIconVisible(boolean visible) {
        Drawable[] drawables = getCompoundDrawablesRelative();
        if (visible) {
            if (drawables[2] == null) {
                setCompoundDrawablesRelative(drawables[0], drawables[1], mDeleteDrawable, drawables[3]);
            }
        } else {
            setCompoundDrawablesRelative(drawables[0], drawables[1], null, drawables[3]);
        }
    }

    public interface OnAfterTextChangedListener {
        void afterTextChanged(String s);
    }

    public interface OnActionSearchListener {
        void actionSearch(String s);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int action = event.getAction();
        if (action == MotionEvent.ACTION_UP) {
            if (getCompoundDrawablesRelative()[2] != null) {
                boolean touchable = event.getX() >= (getWidth() - mDeleteDrawable.getBounds().width() - getPaddingEnd()) && event.getX() <= (getWidth() - getPaddingEnd());
                if (touchable) {
                    this.setText("");
                    return true;
                }
            }
        }
        return super.onTouchEvent(event);
    }
}

